package com.ymatou.iapi.dglogin.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.common.lang.CipherUtil;
import com.ymatou.iapi.dglogin.parameter.AppSourceEnum;
import com.ymatou.iapi.dglogin.parameter.CsWxAuthLoginBean;
import com.ymatou.iapi.dglogin.parameter.UserWxInfo;
import com.ymatou.iapi.dglogin.parameter.WxLoginSession;
import com.ymatou.iapi.dglogin.service.CsWxAuthLoginCall;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.DGThirdPartyBindInfo;
import com.ymttest.database.model.DGUserExtInfo;
import com.ymttest.database.model.DGUserInfo;
import com.ymttest.database.model.DGWxUnionidBindInfo;
import com.ymttest.database.mongo.UserTokenWapper;
import com.ymttest.database.sqlwapper.DGUserWapper;
import com.ymttest.utils.TestDataManager;
/**
*客服注册并且联登
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CsWxAuthLogin{
	private static CsWxAuthLoginBean cswxauthloginBean;
	private static CsWxAuthLoginCall cswxauthloginCall;
	DGUserWapper DGUserWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("客服注册并且联登");
	}

	@Before
	public void caseUp() {
		cswxauthloginBean=new CsWxAuthLoginBean();
		cswxauthloginCall=new CsWxAuthLoginCall();
		DGUserWapper=new DGUserWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(cswxauthloginCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(UserCreateBean usercreateBean2, Integer uid) {
		Logger.comment("验证DGUserInfo");
		DGUserInfo uinfo = DGUserWapper.selectDGUserInfo(uid).get(0);
		//验证是客服
		Logger.verifyEquals(true, uinfo.getIsCs(), "验证IsCs");
		if(usercreateBean2.getWxAppName().equals(DGUserCallService.TestWxsqOther)) {
			Logger.verifyEquals(DGUserCallService.TestWxsqOtherSellerId, uinfo.getBelongSellerId(), "验证BelongSellerId");
		}else {
			Logger.verifyEquals(0, uinfo.getBelongSellerId(), "验证BelongSellerId");
		}
		//loginid随机生成
		Logger.verifyNotNull(uinfo.getLoginId(), "验证LoginId");
		//pwd随机生成
		Logger.verifyNotNull(uinfo.getLoginPwd(), "验证LoginPwd");
		Logger.verifyEquals(usercreateBean2.getLogoUrl(), uinfo.getLogoUrl(), "验证LogoUrl");
		if (usercreateBean2.getMobile() != null) {
			Logger.verifyEquals(usercreateBean2.getMobile(), uinfo.getMobile(), "验证Mobile");
		}
		if (usercreateBean2.getMobileAreaCode() != null) {
			Logger.verifyEquals(usercreateBean2.getMobileAreaCode(), uinfo.getMobileAreaCode(), "验证MobileAreaCode");
		}
		if (usercreateBean2.getBelongSellerId() != null) {
			Logger.verifyEquals(usercreateBean2.getBelongSellerId(), uinfo.getBelongSellerId(), "验证BelongSellerId");
		}
		Logger.verifyEquals(usercreateBean2.getNickname(), uinfo.getNickname(), "验证Nickname");
		Logger.verifyEquals(usercreateBean2.getUserGender()==null?0:usercreateBean2.getUserGender(), uinfo.getUserGender(), "验证UserGender");
		//验证是商家体系
		Logger.verifyEquals(1, uinfo.getUserType(), "验证UserType");
		Logger.verifyEquals(0, uinfo.getUserStatus(), "验证UserStatus");
		Logger.comment("验证DGUserExtInfo");
		DGUserExtInfo exinfo = DGUserWapper.selectDGUserExtInfo(uid).get(0);
		Logger.verifyEquals(usercreateBean2.getAppSource(), exinfo.getAppSource(), "验证AppSource");
		Logger.verifyEquals(usercreateBean2.getChannelSource()==null?0:usercreateBean2.getChannelSource(), exinfo.getChannelSource(), "验证ChannelSource");
		if (usercreateBean2.getClientIp() != null) {
			Logger.verifyEquals(usercreateBean2.getClientIp(), exinfo.getClientIp(), "验证ClientIp");
		}
		//这个接口没有国家编码 要商家注册接口才有
//		Logger.verifyEquals(usercreateBean2.getCountryCode(), exinfo.getCountryCode(), "验证CountryCode");
		//根据uid和oid生成设备号
		Logger.verifyEquals(CipherUtil.encryptMD5(usercreateBean2.getUnionId()), exinfo.getPhysicsDeviceId(), "验证DeviceId");
		Logger.verifyEquals(CipherUtil.encryptMD5(usercreateBean2.getOpenId()), exinfo.getLogicDeviceId(), "验证EquipmentId");
		if (usercreateBean2.getInvitationCode() != null) {
			Logger.verifyEquals(usercreateBean2.getInvitationCode(), exinfo.getInvitationCode(), "验证InvitationCode");
		}
		//目前没有remark？
		Logger.verifyEquals("", exinfo.getRemark(), "验证Remark");
		Logger.verifyEquals(usercreateBean2.getTerminalSource()==null?0:usercreateBean2.getTerminalSource(), exinfo.getTerminalSource(), "验证TerminalSource");
		Logger.comment("验证DGThirdPartyBindInfo");
		DGThirdPartyBindInfo tpinfo = DGUserWapper.selectDGThirdPartyBindInfo(uid).get(0);
		// 多绑定关系在login站点考虑 这里主要看数据落地正确
		Logger.verifyEquals(usercreateBean2.getAppSource(), tpinfo.getAppSource(), "验证AppSource");
		Logger.verifyEquals(usercreateBean2.getOpenId(), tpinfo.getOpenId(), "验证OpenId");
		Logger.verifyEquals(usercreateBean2.getChannelSource()==null?0:usercreateBean2.getChannelSource(), tpinfo.getThirdPartyType(), "验证ThirdPartyType");
		if (usercreateBean2.getUnionId() != null) {
			//oid=uid 为设置uid为null后为了验证设备号而传入的设置参数
			if(!usercreateBean2.getUnionId().equals(usercreateBean2.getOpenId())) {
				Logger.verifyEquals(usercreateBean2.getUnionId(), tpinfo.getUnionId(), "验证UnionId");
			}
		}
		Logger.verifyEquals(1, tpinfo.getUserType(), "验证UserType");

		Logger.comment("验证DGWxUnionidBindInfo");
		List<DGWxUnionidBindInfo> wxuinfo = DGUserWapper.selectDGWxUnionidBindInfo(uid);
		if (wxuinfo.size() > 0) {
			Logger.verifyEquals(1, wxuinfo.get(0).getUserType(), "验证UserType");
			Logger.verifyEquals(usercreateBean2.getUnionId(), wxuinfo.get(0).getUnionId(), "验证UnionId");
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CsWxAuthLogin_001() {
		Logger.start(true,"新客服注册并且联登,成功");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setAppSource(DGUserCallService.DG_SellerWxApp);
			u.setWxAppName(DGUserCallService.TestWxsqSeller);
			//新用户授权联登 等于注册--客服属于商家体系wxsqSeller
			cswxauthloginBean.setIv(TestDataManager.getUTF8Base64(DGUserCallService.IV));
			WxLoginSession wxLoginSession=new WxLoginSession();
			wxLoginSession.setOpenId(u.getOpenId());
			wxLoginSession.setSessionKey(TestDataManager.getUTF8Base64(DGUserCallService.SESSIONKEY));
			wxLoginSession.setUnionId(u.getUnionId());
			String loginSession = DGUserCallService.encryptWxLoginSession(wxLoginSession);
			cswxauthloginBean.setLoginSession(loginSession);
			//加密组装encryptedData
			UserWxInfo UserWxInfo=new UserWxInfo();
			UserWxInfo.setOpenId(u.getOpenId());
			UserWxInfo.setUnionId(u.getUnionId());
			UserWxInfo.setAvatarUrl(u.getLogoUrl());
			UserWxInfo.setNickname(u.getNickname());
			UserWxInfo.setGender(1);
			String encryptedData=DGUserCallService.encryptWxEncryptedData(FormatUtil.beanToGSONString(UserWxInfo), DGUserCallService.IV, DGUserCallService.SESSIONKEY);
			cswxauthloginBean.setEncryptedData(encryptedData);
			cswxauthloginBean.setAppSource(u.getAppSource());
			cswxauthloginBean.setWxAppName(u.getWxAppName());
			cswxauthloginBean.setClientIp(u.getClientIp());
			cswxauthloginBean.setTerminalSource(u.getTerminalSource());
			cswxauthloginCall.setData(cswxauthloginBean);
			cswxauthloginCall.callService();			
			Logger.verifyEquals("true", cswxauthloginCall.getString("success"), "验证success");
			Logger.verifyNotNull(cswxauthloginCall.getString("userId"), "验证uid");
			verifyDB(u, Integer.valueOf(cswxauthloginCall.getString("userId")));
			Map<String, Object> token = new UserTokenWapper().getUserTokenByUid(Integer.valueOf(cswxauthloginCall.getString("userId")));
			Logger.comment(token.toString());
			Logger.verifyEquals(AppSourceEnum.parse(u.getAppSource()).toString().toLowerCase(), token.get("tSource"), "验证tSource");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	//20190709代码优化-存在的openid直接登入
	public void Tc_CsWxAuthLogin_002() {
		Logger.start(true,"已注册客服微信授权联登,失败");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setAppSource(DGUserCallService.DG_SellerWxApp);
			u.setWxAppName(DGUserCallService.TestWxsqSeller);
			//新用户授权联登 等于注册--客服属于商家体系wxsqSeller
			cswxauthloginBean.setIv(TestDataManager.getUTF8Base64(DGUserCallService.IV));
			WxLoginSession wxLoginSession=new WxLoginSession();
			wxLoginSession.setOpenId(u.getOpenId());
			wxLoginSession.setSessionKey(TestDataManager.getUTF8Base64(DGUserCallService.SESSIONKEY));
			wxLoginSession.setUnionId(u.getUnionId());
			String loginSession = DGUserCallService.encryptWxLoginSession(wxLoginSession);
			cswxauthloginBean.setLoginSession(loginSession);
			//加密组装encryptedData
			UserWxInfo UserWxInfo=new UserWxInfo();
			UserWxInfo.setOpenId(u.getOpenId());
			UserWxInfo.setUnionId(u.getUnionId());
			UserWxInfo.setAvatarUrl(u.getLogoUrl());
			UserWxInfo.setNickname(u.getNickname());
			UserWxInfo.setGender(1);
			String encryptedData=DGUserCallService.encryptWxEncryptedData(FormatUtil.beanToGSONString(UserWxInfo), DGUserCallService.IV, DGUserCallService.SESSIONKEY);
			cswxauthloginBean.setEncryptedData(encryptedData);
			cswxauthloginBean.setAppSource(u.getAppSource());
			cswxauthloginBean.setWxAppName(u.getWxAppName());
			cswxauthloginBean.setClientIp(u.getClientIp());
			cswxauthloginBean.setTerminalSource(u.getTerminalSource());
			cswxauthloginCall.setData(cswxauthloginBean);
			cswxauthloginCall.callService();			
			cswxauthloginCall.callService();			
			Logger.verifyEquals("true", cswxauthloginCall.getString("success"), "验证success");
//			Logger.verifyEquals("2", cswxauthloginCall.getString("errorCode"), "验证errorCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CsWxAuthLogin_003() {
		Logger.start(true,"不输入loginSession失败");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setAppSource(DGUserCallService.DG_SellerWxApp);
			u.setWxAppName(DGUserCallService.TestWxsqSeller);
			//新用户授权联登 等于注册--客服属于商家体系wxsqSeller
			cswxauthloginBean.setIv(TestDataManager.getUTF8Base64(DGUserCallService.IV));
			WxLoginSession wxLoginSession=new WxLoginSession();
			wxLoginSession.setOpenId(u.getOpenId());
			wxLoginSession.setSessionKey(TestDataManager.getUTF8Base64(DGUserCallService.SESSIONKEY));
			wxLoginSession.setUnionId(u.getUnionId());
			String loginSession = DGUserCallService.encryptWxLoginSession(wxLoginSession);
//			cswxauthloginBean.setLoginSession(loginSession);
			//加密组装encryptedData
			UserWxInfo UserWxInfo=new UserWxInfo();
			UserWxInfo.setOpenId(u.getOpenId());
			UserWxInfo.setUnionId(u.getUnionId());
			UserWxInfo.setAvatarUrl(u.getLogoUrl());
			UserWxInfo.setNickname(u.getNickname());
			UserWxInfo.setGender(1);
			String encryptedData=DGUserCallService.encryptWxEncryptedData(FormatUtil.beanToGSONString(UserWxInfo), DGUserCallService.IV, DGUserCallService.SESSIONKEY);
			cswxauthloginBean.setEncryptedData(encryptedData);
			cswxauthloginBean.setAppSource(u.getAppSource());
			cswxauthloginBean.setWxAppName(u.getWxAppName());
			cswxauthloginBean.setClientIp(u.getClientIp());
			cswxauthloginBean.setTerminalSource(u.getTerminalSource());
			cswxauthloginCall.setData(cswxauthloginBean);
			cswxauthloginCall.callService();			
			Logger.verifyEquals("false", cswxauthloginCall.getString("success"), "验证success");
			Logger.verifyEquals("1", cswxauthloginCall.getString("errorCode"), "验证errorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CsWxAuthLogin_004() {
		Logger.start(true,"不输入iv失败");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setAppSource(DGUserCallService.DG_SellerWxApp);
			u.setWxAppName(DGUserCallService.TestWxsqSeller);
			//新用户授权联登 等于注册--客服属于商家体系wxsqSeller
//			cswxauthloginBean.setIv(TestDataManager.getUTF8Base64(DGUserCallService.IV));
			WxLoginSession wxLoginSession=new WxLoginSession();
			wxLoginSession.setOpenId(u.getOpenId());
			wxLoginSession.setSessionKey(TestDataManager.getUTF8Base64(DGUserCallService.SESSIONKEY));
			wxLoginSession.setUnionId(u.getUnionId());
			String loginSession = DGUserCallService.encryptWxLoginSession(wxLoginSession);
			cswxauthloginBean.setLoginSession(loginSession);
			//加密组装encryptedData
			UserWxInfo UserWxInfo=new UserWxInfo();
			UserWxInfo.setOpenId(u.getOpenId());
			UserWxInfo.setUnionId(u.getUnionId());
			UserWxInfo.setAvatarUrl(u.getLogoUrl());
			UserWxInfo.setNickname(u.getNickname());
			UserWxInfo.setGender(1);
			String encryptedData=DGUserCallService.encryptWxEncryptedData(FormatUtil.beanToGSONString(UserWxInfo), DGUserCallService.IV, DGUserCallService.SESSIONKEY);
			cswxauthloginBean.setEncryptedData(encryptedData);
			cswxauthloginBean.setAppSource(u.getAppSource());
			cswxauthloginBean.setWxAppName(u.getWxAppName());
			cswxauthloginBean.setClientIp(u.getClientIp());
			cswxauthloginBean.setTerminalSource(u.getTerminalSource());
			cswxauthloginCall.setData(cswxauthloginBean);
			cswxauthloginCall.callService();			
			Logger.verifyEquals("false", cswxauthloginCall.getString("success"), "验证success");
			Logger.verifyEquals("1", cswxauthloginCall.getString("errorCode"), "验证errorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CsWxAuthLogin_005() {
		Logger.start(true,"不输入wxAppName失败");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setAppSource(DGUserCallService.DG_SellerWxApp);
			u.setWxAppName(DGUserCallService.TestWxsqSeller);
			//新用户授权联登 等于注册--客服属于商家体系wxsqSeller
			cswxauthloginBean.setIv(TestDataManager.getUTF8Base64(DGUserCallService.IV));
			WxLoginSession wxLoginSession=new WxLoginSession();
			wxLoginSession.setOpenId(u.getOpenId());
			wxLoginSession.setSessionKey(TestDataManager.getUTF8Base64(DGUserCallService.SESSIONKEY));
			wxLoginSession.setUnionId(u.getUnionId());
			String loginSession = DGUserCallService.encryptWxLoginSession(wxLoginSession);
			cswxauthloginBean.setLoginSession(loginSession);
			//加密组装encryptedData
			UserWxInfo UserWxInfo=new UserWxInfo();
			UserWxInfo.setOpenId(u.getOpenId());
			UserWxInfo.setUnionId(u.getUnionId());
			UserWxInfo.setAvatarUrl(u.getLogoUrl());
			UserWxInfo.setNickname(u.getNickname());
			UserWxInfo.setGender(1);
			String encryptedData=DGUserCallService.encryptWxEncryptedData(FormatUtil.beanToGSONString(UserWxInfo), DGUserCallService.IV, DGUserCallService.SESSIONKEY);
			cswxauthloginBean.setEncryptedData(encryptedData);
			cswxauthloginBean.setAppSource(u.getAppSource());
//			cswxauthloginBean.setWxAppName(u.getWxAppName());
			cswxauthloginBean.setClientIp(u.getClientIp());
			cswxauthloginBean.setTerminalSource(u.getTerminalSource());
			cswxauthloginCall.setData(cswxauthloginBean);
			cswxauthloginCall.callService();			
			Logger.verifyEquals("false", cswxauthloginCall.getString("success"), "验证success");
			Logger.verifyEquals("1", cswxauthloginCall.getString("errorCode"), "验证errorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CsWxAuthLogin_006() {
		Logger.start(true,"不输入appSource失败");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setAppSource(DGUserCallService.DG_SellerWxApp);
			u.setWxAppName(DGUserCallService.TestWxsqSeller);
			//新用户授权联登 等于注册--客服属于商家体系wxsqSeller
			cswxauthloginBean.setIv(TestDataManager.getUTF8Base64(DGUserCallService.IV));
			WxLoginSession wxLoginSession=new WxLoginSession();
			wxLoginSession.setOpenId(u.getOpenId());
			wxLoginSession.setSessionKey(TestDataManager.getUTF8Base64(DGUserCallService.SESSIONKEY));
			wxLoginSession.setUnionId(u.getUnionId());
			String loginSession = DGUserCallService.encryptWxLoginSession(wxLoginSession);
			cswxauthloginBean.setLoginSession(loginSession);
			//加密组装encryptedData
			UserWxInfo UserWxInfo=new UserWxInfo();
			UserWxInfo.setOpenId(u.getOpenId());
			UserWxInfo.setUnionId(u.getUnionId());
			UserWxInfo.setAvatarUrl(u.getLogoUrl());
			UserWxInfo.setNickname(u.getNickname());
			UserWxInfo.setGender(1);
			String encryptedData=DGUserCallService.encryptWxEncryptedData(FormatUtil.beanToGSONString(UserWxInfo), DGUserCallService.IV, DGUserCallService.SESSIONKEY);
			cswxauthloginBean.setEncryptedData(encryptedData);
//			cswxauthloginBean.setAppSource(u.getAppSource());
			cswxauthloginBean.setWxAppName(u.getWxAppName());
			cswxauthloginBean.setClientIp(u.getClientIp());
			cswxauthloginBean.setTerminalSource(u.getTerminalSource());
			cswxauthloginCall.setData(cswxauthloginBean);
			cswxauthloginCall.callService();			
			Logger.verifyEquals("false", cswxauthloginCall.getString("success"), "验证success");
			Logger.verifyEquals("1", cswxauthloginCall.getString("errorCode"), "验证errorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CsWxAuthLogin_007() {
		Logger.start(true,"不输入terminalSource可以成功-默认0");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setAppSource(DGUserCallService.DG_SellerWxApp);
			u.setWxAppName(DGUserCallService.TestWxsqSeller);
			//新用户授权联登 等于注册--客服属于商家体系wxsqSeller
			cswxauthloginBean.setIv(TestDataManager.getUTF8Base64(DGUserCallService.IV));
			WxLoginSession wxLoginSession=new WxLoginSession();
			wxLoginSession.setOpenId(u.getOpenId());
			wxLoginSession.setSessionKey(TestDataManager.getUTF8Base64(DGUserCallService.SESSIONKEY));
			wxLoginSession.setUnionId(u.getUnionId());
			String loginSession = DGUserCallService.encryptWxLoginSession(wxLoginSession);
			cswxauthloginBean.setLoginSession(loginSession);
			//加密组装encryptedData
			UserWxInfo UserWxInfo=new UserWxInfo();
			UserWxInfo.setOpenId(u.getOpenId());
			UserWxInfo.setUnionId(u.getUnionId());
			UserWxInfo.setAvatarUrl(u.getLogoUrl());
			UserWxInfo.setNickname(u.getNickname());
			UserWxInfo.setGender(1);
			String encryptedData=DGUserCallService.encryptWxEncryptedData(FormatUtil.beanToGSONString(UserWxInfo), DGUserCallService.IV, DGUserCallService.SESSIONKEY);
			cswxauthloginBean.setEncryptedData(encryptedData);
			cswxauthloginBean.setAppSource(u.getAppSource());
			cswxauthloginBean.setWxAppName(u.getWxAppName());
			cswxauthloginBean.setClientIp(u.getClientIp());
//			cswxauthloginBean.setTerminalSource(u.getTerminalSource());
			cswxauthloginCall.setData(cswxauthloginBean);
			cswxauthloginCall.callService();			
			Logger.verifyEquals("true", cswxauthloginCall.getString("success"), "验证success");
			Logger.verifyNotNull(cswxauthloginCall.getString("userId"), "验证uid");
			verifyDB(u, Integer.valueOf(cswxauthloginCall.getString("userId")));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CsWxAuthLogin_008() {
		Logger.start(true,"不输入clientIp成功");
		//目前clientip为空对注册无影响
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setClientIp(null);
			u.setAppSource(DGUserCallService.DG_SellerWxApp);
			u.setWxAppName(DGUserCallService.TestWxsqSeller);
			//新用户授权联登 等于注册--客服属于商家体系wxsqSeller
			cswxauthloginBean.setIv(TestDataManager.getUTF8Base64(DGUserCallService.IV));
			WxLoginSession wxLoginSession=new WxLoginSession();
			wxLoginSession.setOpenId(u.getOpenId());
			wxLoginSession.setSessionKey(TestDataManager.getUTF8Base64(DGUserCallService.SESSIONKEY));
			wxLoginSession.setUnionId(u.getUnionId());
			String loginSession = DGUserCallService.encryptWxLoginSession(wxLoginSession);
			cswxauthloginBean.setLoginSession(loginSession);
			//加密组装encryptedData
			UserWxInfo UserWxInfo=new UserWxInfo();
			UserWxInfo.setOpenId(u.getOpenId());
			UserWxInfo.setUnionId(u.getUnionId());
			UserWxInfo.setAvatarUrl(u.getLogoUrl());
			UserWxInfo.setNickname(u.getNickname());
			UserWxInfo.setGender(1);
			String encryptedData=DGUserCallService.encryptWxEncryptedData(FormatUtil.beanToGSONString(UserWxInfo), DGUserCallService.IV, DGUserCallService.SESSIONKEY);
			cswxauthloginBean.setEncryptedData(encryptedData);
			cswxauthloginBean.setAppSource(u.getAppSource());
			cswxauthloginBean.setWxAppName(u.getWxAppName());
//			cswxauthloginBean.setClientIp(u.getClientIp());
			cswxauthloginBean.setTerminalSource(u.getTerminalSource());
			cswxauthloginCall.setData(cswxauthloginBean);
			cswxauthloginCall.callService();			
			Logger.verifyEquals("true", cswxauthloginCall.getString("success"), "验证success");
			Logger.verifyNotNull(cswxauthloginCall.getString("userId"), "验证uid");
			verifyDB(u, Integer.valueOf(cswxauthloginCall.getString("userId")));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CsWxAuthLogin_009() {
		Logger.start(true,"普通用户已经注册过了unionid相同,注册成功");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			//注册普通用户
			Integer uid=DGUserCallService.UserCreateCall(u);
			//设置客服对应appsource和wxappname
			u.setAppSource(DGUserCallService.DG_SellerWxApp);
			u.setWxAppName(DGUserCallService.TestWxsqSeller);
			cswxauthloginBean.setIv(TestDataManager.getUTF8Base64(DGUserCallService.IV));
			WxLoginSession wxLoginSession=new WxLoginSession();
			wxLoginSession.setOpenId(u.getOpenId());
			wxLoginSession.setSessionKey(TestDataManager.getUTF8Base64(DGUserCallService.SESSIONKEY));
			wxLoginSession.setUnionId(u.getUnionId());
			String loginSession = DGUserCallService.encryptWxLoginSession(wxLoginSession);
			cswxauthloginBean.setLoginSession(loginSession);
			//加密组装encryptedData
			UserWxInfo UserWxInfo=new UserWxInfo();
			UserWxInfo.setOpenId(u.getOpenId());
			UserWxInfo.setUnionId(u.getUnionId());
			UserWxInfo.setAvatarUrl(u.getLogoUrl());
			UserWxInfo.setNickname(u.getNickname());
			UserWxInfo.setGender(1);
			String encryptedData=DGUserCallService.encryptWxEncryptedData(FormatUtil.beanToGSONString(UserWxInfo), DGUserCallService.IV, DGUserCallService.SESSIONKEY);
			cswxauthloginBean.setEncryptedData(encryptedData);
			cswxauthloginBean.setAppSource(u.getAppSource());
			cswxauthloginBean.setWxAppName(u.getWxAppName());
			cswxauthloginBean.setClientIp(u.getClientIp());
			cswxauthloginBean.setTerminalSource(u.getTerminalSource());
			cswxauthloginCall.setData(cswxauthloginBean);
			cswxauthloginCall.callService();			
			Logger.verifyEquals("true", cswxauthloginCall.getString("success"), "验证success");
			Logger.verifyNotNull(cswxauthloginCall.getString("userId"), "验证uid");
			verifyDB(u, Integer.valueOf(cswxauthloginCall.getString("userId")));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	//20190709代码优化-存在的openid直接登入
	public void Tc_CsWxAuthLogin_010() {
		Logger.start(true,"商家已经注册过了unionid相同,注册失败");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setIscs(false);
			u.setMobile(TestDataManager.getPhoneNum());
			u.setMobileAreaCode("86");
			u.setUserType(1);
			u.setAppSource(DGUserCallService.DG_SellerWxApp);
			u.setWxAppName(DGUserCallService.TestWxsqSeller);
			//注册普通用户
			Integer uid=DGUserCallService.UserCreateCall(u);
			//设置客服对应appsource和wxappname
			u.setAppSource(DGUserCallService.DG_SellerWxApp);
			u.setWxAppName(DGUserCallService.TestWxsqSeller);
			cswxauthloginBean.setIv(TestDataManager.getUTF8Base64(DGUserCallService.IV));
			WxLoginSession wxLoginSession=new WxLoginSession();
			wxLoginSession.setOpenId(u.getOpenId());
			wxLoginSession.setSessionKey(TestDataManager.getUTF8Base64(DGUserCallService.SESSIONKEY));
			wxLoginSession.setUnionId(u.getUnionId());
			String loginSession = DGUserCallService.encryptWxLoginSession(wxLoginSession);
			cswxauthloginBean.setLoginSession(loginSession);
			//加密组装encryptedData
			UserWxInfo UserWxInfo=new UserWxInfo();
			UserWxInfo.setOpenId(u.getOpenId());
			UserWxInfo.setUnionId(u.getUnionId());
			UserWxInfo.setAvatarUrl(u.getLogoUrl());
			UserWxInfo.setNickname(u.getNickname());
			UserWxInfo.setGender(1);
			String encryptedData=DGUserCallService.encryptWxEncryptedData(FormatUtil.beanToGSONString(UserWxInfo), DGUserCallService.IV, DGUserCallService.SESSIONKEY);
			cswxauthloginBean.setEncryptedData(encryptedData);
			cswxauthloginBean.setAppSource(u.getAppSource());
			cswxauthloginBean.setWxAppName(u.getWxAppName());
			cswxauthloginBean.setClientIp(u.getClientIp());
			cswxauthloginBean.setTerminalSource(u.getTerminalSource());
			cswxauthloginCall.setData(cswxauthloginBean);
			cswxauthloginCall.callService();			
			Logger.verifyEquals("true", cswxauthloginCall.getString("success"), "验证success");
//			Logger.verifyEquals("2", cswxauthloginCall.getString("errorCode"), "验证errorCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CsWxAuthLogin_011() {
		Logger.start(true,"独立小程序用户已经注册过了unionid相同,注册成功");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			//注册普通用户
			u.setAppSource(DGUserCallService.DG_SellerApp);
			u.setWxAppName(DGUserCallService.TestWxsqOther);
			Integer uid=DGUserCallService.UserCreateCall(u);
			//设置客服对应appsource和wxappname
			u.setWxAppName(DGUserCallService.TestWxsqSeller);
			cswxauthloginBean.setIv(TestDataManager.getUTF8Base64(DGUserCallService.IV));
			WxLoginSession wxLoginSession=new WxLoginSession();
			wxLoginSession.setOpenId(u.getOpenId());
			wxLoginSession.setSessionKey(TestDataManager.getUTF8Base64(DGUserCallService.SESSIONKEY));
			wxLoginSession.setUnionId(u.getUnionId());
			String loginSession = DGUserCallService.encryptWxLoginSession(wxLoginSession);
			cswxauthloginBean.setLoginSession(loginSession);
			//加密组装encryptedData
			UserWxInfo UserWxInfo=new UserWxInfo();
			UserWxInfo.setOpenId(u.getOpenId());
			UserWxInfo.setUnionId(u.getUnionId());
			UserWxInfo.setAvatarUrl(u.getLogoUrl());
			UserWxInfo.setNickname(u.getNickname());
			UserWxInfo.setGender(1);
			String encryptedData=DGUserCallService.encryptWxEncryptedData(FormatUtil.beanToGSONString(UserWxInfo), DGUserCallService.IV, DGUserCallService.SESSIONKEY);
			cswxauthloginBean.setEncryptedData(encryptedData);
			cswxauthloginBean.setAppSource(u.getAppSource());
			cswxauthloginBean.setWxAppName(u.getWxAppName());
			cswxauthloginBean.setClientIp(u.getClientIp());
			cswxauthloginBean.setTerminalSource(u.getTerminalSource());
			cswxauthloginCall.setData(cswxauthloginBean);
			cswxauthloginCall.callService();			
			Logger.verifyEquals("true", cswxauthloginCall.getString("success"), "验证success");
			Logger.verifyNotNull(cswxauthloginCall.getString("userId"), "验证uid");
			verifyDB(u, Integer.valueOf(cswxauthloginCall.getString("userId")));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CsWxAuthLogin_012() {
		Logger.start(true,"输入wxAppName错误-普通用户小程序appname-失败");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setAppSource(DGUserCallService.DG_SellerWxApp);
			u.setWxAppName(DGUserCallService.TestWxsqBuyer);
			//新用户授权联登 等于注册--客服属于商家体系wxsqSeller
			cswxauthloginBean.setIv(TestDataManager.getUTF8Base64(DGUserCallService.IV));
			WxLoginSession wxLoginSession=new WxLoginSession();
			wxLoginSession.setOpenId(u.getOpenId());
			wxLoginSession.setSessionKey(TestDataManager.getUTF8Base64(DGUserCallService.SESSIONKEY));
			wxLoginSession.setUnionId(u.getUnionId());
			String loginSession = DGUserCallService.encryptWxLoginSession(wxLoginSession);
			cswxauthloginBean.setLoginSession(loginSession);
			//加密组装encryptedData
			UserWxInfo UserWxInfo=new UserWxInfo();
			UserWxInfo.setOpenId(u.getOpenId());
			UserWxInfo.setUnionId(u.getUnionId());
			UserWxInfo.setAvatarUrl(u.getLogoUrl());
			UserWxInfo.setNickname(u.getNickname());
			UserWxInfo.setGender(1);
			String encryptedData=DGUserCallService.encryptWxEncryptedData(FormatUtil.beanToGSONString(UserWxInfo), DGUserCallService.IV, DGUserCallService.SESSIONKEY);
			cswxauthloginBean.setEncryptedData(encryptedData);
			cswxauthloginBean.setAppSource(u.getAppSource());
			cswxauthloginBean.setWxAppName(u.getWxAppName());
			cswxauthloginBean.setClientIp(u.getClientIp());
			cswxauthloginBean.setTerminalSource(u.getTerminalSource());
			cswxauthloginCall.setData(cswxauthloginBean);
			cswxauthloginCall.callService();			
			Logger.verifyEquals("false", cswxauthloginCall.getString("success"), "验证success");
			Logger.verifyEquals("2", cswxauthloginCall.getString("errorCode"), "验证errorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CsWxAuthLogin_013() {
		Logger.start(true,"输入wxAppName错误-独立用户小程序appname-失败");
		try {
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setAppSource(DGUserCallService.DG_SellerWxApp);
			u.setWxAppName(DGUserCallService.TestWxsqOther);
			//新用户授权联登 等于注册--客服属于商家体系wxsqSeller
			cswxauthloginBean.setIv(TestDataManager.getUTF8Base64(DGUserCallService.IV));
			WxLoginSession wxLoginSession=new WxLoginSession();
			wxLoginSession.setOpenId(u.getOpenId());
			wxLoginSession.setSessionKey(TestDataManager.getUTF8Base64(DGUserCallService.SESSIONKEY));
			wxLoginSession.setUnionId(u.getUnionId());
			String loginSession = DGUserCallService.encryptWxLoginSession(wxLoginSession);
			cswxauthloginBean.setLoginSession(loginSession);
			//加密组装encryptedData
			UserWxInfo UserWxInfo=new UserWxInfo();
			UserWxInfo.setOpenId(u.getOpenId());
			UserWxInfo.setUnionId(u.getUnionId());
			UserWxInfo.setAvatarUrl(u.getLogoUrl());
			UserWxInfo.setNickname(u.getNickname());
			UserWxInfo.setGender(1);
			String encryptedData=DGUserCallService.encryptWxEncryptedData(FormatUtil.beanToGSONString(UserWxInfo), DGUserCallService.IV, DGUserCallService.SESSIONKEY);
			cswxauthloginBean.setEncryptedData(encryptedData);
			cswxauthloginBean.setAppSource(u.getAppSource());
			cswxauthloginBean.setWxAppName(u.getWxAppName());
			cswxauthloginBean.setClientIp(u.getClientIp());
			cswxauthloginBean.setTerminalSource(u.getTerminalSource());
			cswxauthloginCall.setData(cswxauthloginBean);
			cswxauthloginCall.callService();			
			Logger.verifyEquals("false", cswxauthloginCall.getString("success"), "验证success");
			Logger.verifyEquals("2", cswxauthloginCall.getString("errorCode"), "验证errorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CsWxAuthLogin_014() {
		Logger.start(true,"新客服注册并且联登-拦截邀请客服-不返回token");
		try {
			Integer mchId= GodmchManageCallService.CreateSellerMch();
			UserCreateBean u= DGUserCallService.initUsercreateBean();
			u.setAppSource(DGUserCallService.DG_SellerWxApp);
			u.setWxAppName(DGUserCallService.TestWxsqSeller);
			//新用户授权联登 等于注册--客服属于商家体系wxsqSeller
			cswxauthloginBean.setIv(TestDataManager.getUTF8Base64(DGUserCallService.IV));
			WxLoginSession wxLoginSession=new WxLoginSession();
			wxLoginSession.setOpenId(u.getOpenId());
			wxLoginSession.setSessionKey(TestDataManager.getUTF8Base64(DGUserCallService.SESSIONKEY));
			wxLoginSession.setUnionId(u.getUnionId());
			String loginSession = DGUserCallService.encryptWxLoginSession(wxLoginSession);
			cswxauthloginBean.setLoginSession(loginSession);
			//加密组装encryptedData
			UserWxInfo UserWxInfo=new UserWxInfo();
			UserWxInfo.setOpenId(u.getOpenId());
			UserWxInfo.setUnionId(u.getUnionId());
			UserWxInfo.setAvatarUrl(u.getLogoUrl());
			UserWxInfo.setNickname(u.getNickname());
			UserWxInfo.setGender(1);
			String encryptedData=DGUserCallService.encryptWxEncryptedData(FormatUtil.beanToGSONString(UserWxInfo), DGUserCallService.IV, DGUserCallService.SESSIONKEY);
			cswxauthloginBean.setEncryptedData(encryptedData);
			cswxauthloginBean.setAppSource(u.getAppSource());
			cswxauthloginBean.setWxAppName(u.getWxAppName());
			cswxauthloginBean.setClientIp(u.getClientIp());
			cswxauthloginBean.setTerminalSource(u.getTerminalSource());
			cswxauthloginCall.setData(cswxauthloginBean);
			cswxauthloginCall.callService();			
			Logger.verifyEquals("true", cswxauthloginCall.getString("success"), "验证success");
			Logger.verifyNotNull(cswxauthloginCall.getString("userId"), "验证uid");
			Logger.verifyNotNull(cswxauthloginCall.getString("accessToken"), "验证accessToken");
			verifyDB(u, Integer.valueOf(cswxauthloginCall.getString("userId")));
			Map<String, Object> token = new UserTokenWapper().getUserTokenByUid(Integer.valueOf(cswxauthloginCall.getString("userId")));
			Logger.comment(token.toString());
			Logger.verifyEquals(AppSourceEnum.parse(u.getAppSource()).toString().toLowerCase(), token.get("tSource"), "验证tSource");
			Integer csid=Integer.valueOf(cswxauthloginCall.getString("userId"));
			GodmchManageCallService.CreateCSCall(mchId, csid, 11);
			cswxauthloginCall.callService();			
			Logger.verifyEquals("false", cswxauthloginCall.getString("success"), "验证success");
			Logger.verifyIsNull(cswxauthloginCall.getJsonObject().get("accessToken"), "验证accessToken");
			//对不起，该账号暂无权限登录后台
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}